package com.parleys.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import roboguice.inject.InjectView;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.parleys.R;
import com.parleys.adapter.ModeSelectionListAdapter;
import com.parleys.adapter.ParleysListAdapter;
import com.parleys.domain.ModeSelectionItem;
import com.parleys.domain.ParleysListObject;
import com.parleys.helper.ApplicationConstants;

/**
 * Gives a user the posibility to choose between online and offline mode. When to user has made a choice he's brought to
 * the list of spaces.
 * 
 * @author David Van Droogenbroeck
 * 
 */
public class ModeSelectionActivity extends AbstractParleysActivity {

	private static final String TAG = ModeSelectionActivity.class.getName();

	private static Boolean SPLASH_SHOWN = false;

	@InjectView(R.id.list)
	private ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		showSplashWhenNeeded();

		setContentView(R.layout.parleys_list);

		setTitle(getString(R.string.online_offline));

		List<ParleysListObject> list = new ArrayList<ParleysListObject>();
		list.add(createOlineModeSelectionItem());
		list.add(createOfflineModeSelectionItem());

		listView.setAdapter(new ModeSelectionListAdapter(this, list));
		listView.setOnItemClickListener(new ModeOnClickHandler());

	}

	private void showSplashWhenNeeded() {
		if (!SPLASH_SHOWN) {
			Intent splashIntent = new Intent(ModeSelectionActivity.this, SplashActivity.class);
			startActivityForResult(splashIntent, 0);
			SPLASH_SHOWN = true;
		}
	}

	private ModeSelectionItem createOlineModeSelectionItem() {
		ModeSelectionItem online = new ModeSelectionItem();
		online.setName(getString(R.string.online));
		online.setDescription(getString(R.string.online_description));
		online.setThumbnail(getString(R.string.online));
		return online;
	}

	private ModeSelectionItem createOfflineModeSelectionItem() {
		ModeSelectionItem offline = new ModeSelectionItem();
		offline.setName(getString(R.string.offline));
		offline.setDescription(getString(R.string.offline_description));
		offline.setThumbnail(getString(R.string.offline));
		return offline;
	}

	private class ModeOnClickHandler implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> listview, View layout, int arg2, long arg3) {
			final ModeSelectionItem modeSelectionItem = (ModeSelectionItem) ((ParleysListAdapter) listview.getAdapter())
					.getItem(arg2);
			Intent intent = new Intent(ModeSelectionActivity.this, SpacesListActivity.class);
			intent.putExtra(ApplicationConstants.ONLINE_ARG, getString(R.string.online).equals(
					modeSelectionItem.getName()));

			ModeSelectionActivity.this.startActivity(intent);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem resetMenuItem = menu.add(0, 0, 0, getString(R.string.reset));
		resetMenuItem.setIcon(android.R.drawable.ic_menu_revert);
		MenuItem aboutMenuItem = menu.add(1, 1, 1, getString(R.string.about));
		aboutMenuItem.setIcon(android.R.drawable.ic_menu_help);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 0) {
			showResetDialog();
		} else if (item.getItemId() == 1) {
			showAboutDialog();
		}

		return false;
	}

	private void showResetDialog() {
		new AlertDialog.Builder(this) //
				.setIcon(android.R.drawable.ic_dialog_alert) //
				.setTitle(getString(R.string.reset_application_title)) //
				.setMessage(getString(R.string.reset_application_description)) //
				.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						resetApplication();
					}

				}).setNegativeButton(getString(R.string.no), null) //
				.show();
	}

	private void showAboutDialog() {

		String versionName = null;
		try {
			PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0);
			versionName = pi.versionName;
		} catch (PackageManager.NameNotFoundException e) {
		}

		View view = getLayoutInflater().inflate(R.layout.about, null, false);

		TextView version = (TextView) view.findViewById(R.id.version);
		version.setText(this.getString(R.string.version, versionName));

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.app_name_full));
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setView(view);
		builder.setInverseBackgroundForced(true);
		builder.setPositiveButton(getString(android.R.string.ok), null);
		builder.setCancelable(true);
		builder.create().show();
	}

	private void resetApplication() {
		try {
			FileUtils.cleanDirectory(ApplicationConstants.EXTERNAL_STORAGE_DIRECTORY);
			Toast toast = Toast.makeText(this, getString(R.string.reset_application_done), 3);
			toast.show();
		} catch (IOException e) {
			Log.e(TAG, "Error while resetting application", e);
		}
	}

}
